using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class ProductCategory
    {
          #region Common
        public eProductCategory  Data;

        public ProductCategory()
        {
            Data = new eProductCategory ();
		}

        ~ProductCategory()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
           cProductCategory objDAL = new cProductCategory();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
           cProductCategory objDAL = new cProductCategory();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
           cProductCategory objInfor = new cProductCategory(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.ProductCategoryID ;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
           cProductCategory objInfor = new cProductCategory();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

      

        private void MappingData(cProductCategory objInfor)
        {
           
            try
            {
                objInfor.ProductCategoryName1 = Data.ProductCategoryName1;
            }
            catch
            { objInfor.ProductCategoryName1 = ""; }
            try
            {
                objInfor.ProductCategoryName2 = Data.ProductCategoryName2;
            }
            catch
            { objInfor.ProductCategoryName2= ""; }
            try
            {
                objInfor.ProductCategoryName3 = Data.ProductCategoryName3;
            }
            catch
            { objInfor.ProductCategoryName3 = ""; }
            try {
                objInfor.ProductCategoryCode = Data.ProductCategoryCode;
            }
            catch
            {  objInfor.ProductCategoryCode ="";}
            objInfor.Parent = Data.Parent;
            objInfor.Priority = Data.Priority; 
            
        }

        private void ConvertData(cProductCategory objInfor)
        {
            Data.ProductCategoryName1 = objInfor.ProductCategoryName1;
            Data.ProductCategoryName2 = objInfor.ProductCategoryName2;
            Data.ProductCategoryName3 = objInfor.ProductCategoryName3;
            Data.ProductCategoryCode = objInfor.ProductCategoryCode;
            Data.Parent = objInfor.Parent;
            Data.Priority = objInfor.Priority;
        }
       
        #endregion

        public DataTable Search()
        {
            DataTable tbResult = new DataTable();
            cProductCategory objDAL = new cProductCategory();
            tbResult = objDAL.Search();
            return tbResult;
        }

        public DataTable SearchByParent(int Parent)
        {
            DataTable tbResult = new DataTable();
            cProductCategory objDAL = new cProductCategory();
            tbResult = objDAL.SearchByParent(Parent);
            return tbResult;
        }

        public bool CheckExistProductCode(string ProductCode)
        {
            bool isExist = false;
            cProductCategory objDAL = new cProductCategory();
            isExist = objDAL.CheckExistProductCode(ProductCode);
            return isExist;
        }
    }
}
